﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using WatiN.Sharepoint.WebControls;

namespace WatiN.Sharepoint.Pages.CentralAdmin
{
    public class CreateSitePage : BaseCentralAdminPage
    {
        public CreateSitePage()
            : base()
        {}

        public override string RelativePageUrl
        {
            get { return @"_admin/createsite.aspx"; }
        }
        
        public TextField SiteTitle
        {
            get
            {
                return Document.TextField(new Regex("TxtCreateSiteTitle$"));
            }
        }

        public TextField SiteDescription
        {
            get
            {
                return Document.TextField(new Regex("TxtCreateSiteDescription$"));
            }
        }

        public TextField SiteName
        {
            get
            {
                return Document.TextField(new Regex("TxtSiteName$"));
            }
        }

        public SelectList URL
        {
            get
            {
                return Document.SelectList(new Regex("DdlWildcardInclusion$"));
            }
        }

        public SelectList Language
        {
            get
            {
                return Document.SelectList(new Regex("DdLanguageWebTemplate$"));
            }
        }

        public WatiN.Core.Div FilterTabs
        {
            get
            {
                return Document.Div(new Regex("FilterTabs$"));
            }
        }

        public void SelectWebTemplateGroup(string name)
        {
            FilterTabs.Link(Find.ByText(name)).Click();
        }


        public SelectList WebTemplate
        {
            get
            {
                return Document.SelectList(new Regex("LbWebTemplate$"));
            }
        }

        public void SetPrimaryAdministrator(string name)
        {
            PeoplePicker primaryAsTextField = Document.Control<PeoplePicker>(new Regex("PickerOwner$"));            

            primaryAsTextField.TypeText(name);
            
        }

        public void SetSecondaryAdministrator(string name)
        {
            PeoplePicker primaryAsTextField = Document.Control<PeoplePicker>(new Regex("PickerOwner2$"));

            primaryAsTextField.TypeText(name);

        }

        public void Submit(bool waitForResults)
        {
            Button submitButton = Document.Button(new Regex("BtnCreateSite$"));
            
            if (waitForResults)
            {
                submitButton.ClickNoWait();

                Document.WaitUntilContainsText("The new top-level site was created successfully", 120);

                if (Document.ContainsText("The new top-level site was created successfully") == false)
                {
                    throw new ApplicationException("De site kan niet (op tijd) worden aangemaakt");
                }
            }
            else
            {
                submitButton.ClickNoWait();
            }
        }
    }
}
